package com.frxuexi.mdm2203.view

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.frxuexi.apps.view.AppsPage
import com.frxuexi.mdm2203.MyNavHost
import com.frxuexi.mdm2203.R
import com.frxuexi.mdm2203.demo.Demo03
import com.frxuexi.mdm2203.demo.Greeting

sealed class NavItem(val title: String, val route: String, val img: Int) {
    data object MessagePage : NavItem("消息", "message", R.drawable.icon1275)
    data object HomePage : NavItem("门户", "home", R.drawable.icon64)
    data object MinePage : NavItem("我", "mine", R.drawable.icon64)
}

@Composable
fun MainPage(navHostController: NavHostController = rememberNavController()) {
    val list = listOf<NavItem>(NavItem.MessagePage, NavItem.HomePage, NavItem.MinePage)

    val navBackEntity by navHostController.currentBackStackEntryAsState()
    val destination = navBackEntity?.destination

    Scaffold(bottomBar = {
        //只有在list中的item页面才显示底部导航
        if (destination?.hierarchy?.any {
                list.map { navItem -> navItem.route }.contains(it.route)
            } == true) {
            NavigationBar {
                //底部导航
                list.forEach { nav ->
                    NavigationBarItem(selected = destination?.hierarchy?.any { it.route == nav.route } == true,
                        onClick = {
                            navHostController.navigate(nav.route) {
                                popUpTo(navHostController.graph.startDestinationId) {
                                    saveState = true//保持状态
                                }
                                launchSingleTop = true//栈顶复用
                                restoreState = true//回复状态
                            }
                        },
                        icon = {
                            Icon(
                                painter = painterResource(id = nav.img),
                                contentDescription = ""
                            )
                        },
                        label = { Text(text = nav.title) }
                    )
                }
            }
        }
    }) {
        MyNavHost(navHostController = navHostController, it = it)
    }
}